home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / c_spec / execute / date.c < prev    next >
C/C++ Source or Header  |  1986-02-20  |  3KB  |  126 lines

  1. #include <stdio.h>
  2. #include "/include/mydos.h"
  3.  
  4. #undef min
  5.  
  6. /*    DATE.C    Get the date from dos
  7.  *
  8.  *    Copyright (C) 1986, Allen I. Holub. All rights reserved.
  9.  */
  10.  
  11. #define E(x)    fprintf(stderr,"%s\n", x );
  12.  
  13. extern void    mydos( REGS * );    /* In mydos.asm        */
  14. extern void    gregs( REGS * );
  15.  
  16. /*----------------------------------------------------------------------*/
  17.  
  18. static char    *weekdays[] =
  19. {
  20.     "Sun",
  21.     "Mon",
  22.     "Tue",
  23.     "Wed",
  24.     "Thu",
  25.     "Fri",
  26.     "Sat"
  27. };
  28.  
  29. static char    *months[] =
  30. {
  31.     "",        /* There is no month 0 */
  32.     "Jan",
  33.     "Feb",
  34.     "Mar",
  35.     "Apr",
  36.     "May",
  37.     "Jun",
  38.     "Jul",
  39.     "Aug",
  40.     "Sep",
  41.     "Oct",
  42.     "Nov",
  43.     "Dec"
  44. };
  45.  
  46. /*----------------------------------------------------------------------*/
  47.  
  48. date( month, day, year, day_of_the_week )
  49. int    *month, *day, *year, *day_of_the_week ;
  50. {
  51.     /*    Return the month, day, year, and day of the week (0 = sunday,
  52.      *    6 = saturday). This function is totally non-portable,
  53.      *    and must be compiled with the lattice C compiler.
  54.      */
  55.  
  56.     REGS    regs;
  57.  
  58.     gregs(®s);
  59.     regs.h.ah = 0x2a ;
  60.     mydos( ®s );
  61.  
  62.     *month         = regs.h.dh ;
  63.     *day         = regs.h.dl ;
  64.     *year         = regs.x.cx ;
  65.     *day_of_the_week = regs.h.al ;
  66. }
  67.  
  68. /*----------------------------------------------------------------------*/
  69.  
  70. time( hr, min, sec, hundredths )
  71. int *hr, *min, *sec, *hundredths;
  72. {
  73.     /*    Return the time. This is probably non-portable
  74.      */
  75.  
  76.     REGS    regs ;
  77.  
  78.     gregs( ®s );
  79.     regs.h.ah = 0x2c ;
  80.     mydos( ®s );
  81.  
  82.     *hr        = regs.h.ch ;
  83.     *min        = regs.h.cl ;
  84.     *sec        = regs.h.dh ;
  85.     *hundredths = regs.h.dl ;
  86. }
  87.  
  88. /*----------------------------------------------------------------------*/
  89. #ifdef DEBUG
  90.  
  91. main()
  92. {
  93.     int    hr, min, sec, hundredths;
  94.  
  95.     time( &hr, &min, &sec, &hundredths );
  96.  
  97.     printf("time is %d:%d:%d.%d\n", hr, min, sec, hundredths );
  98. }
  99.  
  100. #endif
  101. /*----------------------------------------------------------------------*/
  102.  
  103. main(argc, argv)
  104. char    **argv;
  105. {
  106.     static int hr, min, sec, hund, mo, day, yr, dow ;
  107.  
  108.     time( &hr, &min, &sec, &hund );
  109.     date( &mo, &day, &yr,  &dow  );
  110.  
  111.     if( argc <= 1 )
  112.         printf("%s. %s %02d, %d     %02d:%02d:%02d\n",
  113.             weekdays[dow], months[mo], day, yr, hr, min, sec);
  114.  
  115.     else if( argv[1][0] == '-' && argv[1][1] == 'n' )
  116.         printf("%02d/%02d/%02d %02d:%02d:%02d\n",
  117.                            yr, mo, day, hr, min, sec);
  118.     else
  119.     {
  120.         E("Date: Copyright (c) 1986, Allen I. Holub. All rights reserved.");
  121.         E("\nUsage: date [-n]\n");
  122.         E("Print the date and time in English by default, in numeric");
  123.         E("format (YY:MM:DD HH:MM:SS) if -n is given on command line.");
  124.     }
  125. }
  126.